package cn.jianml.swagger2.controller;

import cn.jianml.swagger2.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户管理相关接口
 * @author: wujian
 * @time: 2019/11/11
 */
@Api(tags = "用户管理相关接口")
@RestController
@RequestMapping("/user")
public class UserController {

    private static Map<Integer, User> userMap = new HashMap<>();

    @ApiIgnore() // 添加这个注解将不会显示在UI界面上
    @ApiOperation(value = "接口的功能介绍", notes = "提示接口使用者注意事项")
    @GetMapping("/hello")
    public String hello(){
        return "hello world！";
    }

    @ApiOperation("获取用户列表接口")
    @GetMapping("/list")
    public List<User> listUser(){
        return new ArrayList<>(userMap.values());
    }

    @ApiOperation("添加用户的接口")
    @ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, paramType = "body", dataType = "User")
    @PostMapping("/")
    public void addUser(@RequestBody User user){
        userMap.put(user.getId(), user);
    }

    @ApiOperation("更新用户的接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户id", required = true, paramType = "path", dataType = "String"),
            @ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, paramType = "body", dataType = "User")
    })
    @PutMapping("/{id}")
    public void updateUser(@PathVariable Integer id, @RequestBody User user) {
        userMap.remove(id);
        userMap.put(user.getId(), user);
    }

    @ApiOperation("根据id查询用户的接口")
    @ApiImplicitParam(name = "id", value = "用户ID", defaultValue = "1", required = true, paramType = "path", dataType = "String")
    @GetMapping("/{id}")
    public User getUser(@PathVariable Integer id){
        if(!userMap.containsKey(id))
            return null;
        return userMap.get(id);

    }

    @ApiOperation("根据id删除用户的接口")
    @ApiImplicitParam(name = "id", value = "用户ID", defaultValue = "1", required = true, paramType = "path", dataType = "String")
    @DeleteMapping("/{id}")
    public void deleteUser(@PathVariable Integer id) {
        userMap.remove(id);
    }

}
